<?php
/**
 * @Author: kendy
 * @Date:    2020-04-22 16:03:42
 * @Last Modified by:   jiayanting
 * @Last Modified time: 2020-04-23 10:18:59
 */
namespace app\api\job;

use think\queue\Job;
use think\Log;
use think\Db;
use think\loader;
class addFalseApp {

    /**
     * fire是消息队列默认调用的方法
     * @param Job $job 当前的任务对象
     * @param array|mixed $data 发布任务时自定义的数据
     */
    public function fire(Job $job, $data)
    { 
        $content = Db::name('quick_comments')
        ->where(['is_display'=>0,'is_del'=>0])
        ->orderRaw('rand()') 
        ->value("content"); 
        if($data['type']==1){
            //插入到数据库
            $date = [
                'uid'=>$data['uid'],
                'wid'=>$data['object_id'], 
                'content'=>$content,
                'is_real'=>0, 
                'time'=>date('Y-m-d H:i:s')
            ];
         $res = Db::name('comment_method')->insert($date);
         if(false !== $res){
              $artcileInfo = Db::name('method')->field('uid')->where('mid',$data['object_id'])->find(); 
                //插入消息 
                    $messageData = [
                        'type'=> 1,
                        'wid' =>  $data['object_id'],
                        'time' =>date("Y-m-d H:i:s"),
                        'kinds'=>1,
                        'comment_content' => $content,
                        'comment_uid' =>  $data['uid'],
                        'uid'=>$artcileInfo['uid']
                    ];
              $info =  Db::name('comment_method_message')->insert($messageData);
         }
        } 

        if($data['type']==2){
            //插入到数据库
            $date = [
                'uid'=>$data['uid'],
                'wid'=>$data['object_id'], 
                'content'=>$content,
                'is_real'=>0, 
                'time'=>date('Y-m-d H:i:s')
            ];
             $res = Db::name('comment_circle')->insert($date); 
           if(false !== $res){
              $artcileInfo = Db::name('friends_circle')->field('uid')->where('f_id',$data['object_id'])->find(); 
                //插入消息 
                    $messageData = [
                        'type'=> 1,
                        'wid' => $data['object_id'],
                        'time' =>date("Y-m-d H:i:s"),
                        'kinds'=>2,
                        'comment_content' => $content,
                        'comment_uid' => $data['uid'],
                        'uid'=>$artcileInfo['uid']
                    ];
              $info =  Db::name('comment_message')->insert($messageData);
          }  
        }
       
        if($info){
            echo '成功';
            $job->delete();

        }else{
            //任务执行超过1次，则删除任务
            if ($job->attempts() > 3) {
                echo "执行次数超过3次";
                $job->delete();
            }
        }

        if ($job->attempts() > 3) {
            echo "执行次数超过3次";
            $job->delete();
        }
        $job->delete();
    }
    /**
     * 消息在到达消费者时可能已经不需要执行了
     * @param array|mixed $data 发布任务时自定义的数据
     * @return boolean 任务执行的结果
     */
    private function checkJob($data)
    {

        return true;
    }
    /**
     * 根据消息中的数据进行实际的业务处理
     */
    private function doJob($data)
    {
        //判断当前时间是否
        return true;
        // 实际业务流程处理

    }
}
